package com.jinlejia.erp.ui.mine.assist;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.ui.common.CommonDeleteDialog;
import com.jinlejia.erp.ui.common.ProgressDialog;
import com.jinlejia.erp.ui.customer.DetailCustomerActivity;
import com.jinlejia.erp.ui.house.HouseDetailActivity;
import com.jinlejia.erp.ui.mine.adapter.AssistInfoAdapter;
import com.jinlejia.erp.utils.DataFilter;
import com.jinlejia.erp.utils.StringUtil;
import com.jinlejia.erp.widget.MyListView;
import com.zhy.http.okhttp.OkHttpUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 协助签单详情
 */
public class AssistInfoActivity extends AppCompatActivity implements View.OnClickListener {

    TextView tvHouseNumber;
    TextView tvName;
    LinearLayout btnConfirm;
    ImageView backBtn;
    MyListView yfListview;

    ArrayList<Map<String, Object>> dataList = new ArrayList<>();
    private AssistInfoAdapter adapter;
    private View lvHead;
    private String id;
    private String typeData[] = {"二手房", "租赁", "一手房", "垫资"};
    private String shureData[] = {"待确认", "已确认"};
    //协作类型
    private TextView tvType;
    private TextView tvTime;
    private TextView tvIsShure;
    //协作类型
    private int type;
    private String rscode;
    private ProgressDialog progressDialog;

    private String sex;
    private String cscode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assist_info);
        initView();
        initData();
        initListener();

    }

    private void initListener() {
        tvHouseNumber.setOnClickListener(this);
        tvName.setOnClickListener(this);
        backBtn.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);
    }

    private void initData() {
        progressDialog = new ProgressDialog(this, "加载中...");
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            Uri uri = intent.getData();
            if (uri != null) {
                id = uri.getQueryParameter("code");
            }
        }
        getData();
    }

    private void getData() {
        progressDialog.show();
        OkHttpUtils
                .get()
                .url(ApiConfig.ASSIST_INFO)
                .addParams("id", id)
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        if (DataFilter.filter(AssistInfoActivity.this, response, null)) {
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            Map<String, Object> map = JSON.parseObject(response.get("data").toString(), new TypeReference<Map<String, Object>>() {
                            });
                            rscode = map.get("rscode").toString();
                            cscode = map.get("cscode").toString();
                            tvHouseNumber.setText(rscode);//房源编号
                            tvName.setText(map.get("customername").toString());//客户姓名
                            type = Integer.parseInt(map.get("type").toString());//协作类型int
                            sex = map.get("sex").toString();
                            tvType.setText(typeData[type]);//协作类型
                            tvTime.setText(sdf.format(new Date(Long.valueOf(map.get("initiateTime").toString()))));//申请日期
                            if (map.containsKey("state") && !TextUtils.isEmpty(StringUtil.valueOf(map.get("state"))))
                                tvIsShure.setText(shureData[Integer.parseInt(map.get("state").toString())]);//确认状态--个人的

                            if ("0".equals(map.get("state").toString())) {//待确认
                                btnConfirm.setVisibility(View.VISIBLE);
                            } else {
                                btnConfirm.setVisibility(View.GONE);
                            }
                            dataList = JSON.parseObject(data.getJSONArray("cooperationuser").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            adapter.setData(dataList);
                        }
                    }
                });
    }

    private JSONObject jsonObject = new JSONObject();

    /**
     * 确认信息
     */
    private void confirmData() {
        jsonObject.put("id", id);
        progressDialog.show();
        OkHttpUtils
                .postString()
                .url(ApiConfig.ASSIST_CONFIRM)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(jsonObject.toString())
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
//                        String message = response.get("message").toString();
                        if (DataFilter.filter(AssistInfoActivity.this, response, null)) {
                            //确认成功
                            btnConfirm.setVisibility(View.GONE);
                            getData();
                        }
                    }
                });
    }

    private void initView() {
        yfListview = ((MyListView) findViewById(R.id.yf_listview));

        adapter = new AssistInfoAdapter(getApplication(), dataList);
        lvHead = View.inflate(this, R.layout.head_assistinfo, null);
        yfListview.addHeaderView(lvHead);
        tvHouseNumber = ((TextView) findViewById(R.id.tv_houseNumber));
        tvName = ((TextView) findViewById(R.id.tv_name));
        btnConfirm = ((LinearLayout) findViewById(R.id.btn_confirm));
        backBtn = ((ImageView) findViewById(R.id.back_btn));
        tvType = ((TextView) findViewById(R.id.tv_type));
        tvTime = ((TextView) findViewById(R.id.tv_time));
        tvIsShure = ((TextView) findViewById(R.id.tv_isShure));
        yfListview.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_houseNumber:
                Intent intent = new Intent();
                if (1 == type) {//租赁
                    intent.putExtra("type", "rent");
                } else {//买卖
                    intent.putExtra("type", "shs");
                }
                intent.putExtra("code", rscode);
                intent.setClass(AssistInfoActivity.this, HouseDetailActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_name:
                Intent intent1 = new Intent();
                if (1 == type) {//租赁
                    intent1.putExtra("type", "rent");
                } else {//买卖
                    intent1.putExtra("type", "sale");
                }
                intent1.putExtra("csCode", cscode);
                intent1.putExtra("sex", sex);
                intent1.putExtra("name", tvName.getText().toString());
                intent1.setClass(AssistInfoActivity.this, DetailCustomerActivity.class);
                startActivity(intent1);
                break;
            case R.id.btn_confirm://确认信息
                CommonDeleteDialog deleteDialog = new CommonDeleteDialog(this, "确认信息", "是否确认信息？");
                deleteDialog.show();
                deleteDialog.setConfirmListener(new CommonDeleteDialog.OnConfirmListener() {
                    @Override
                    public void confirm() {
//                        Toast.makeText(AssistInfoActivity.this,"确认",Toast.LENGTH_SHORT).show();
                        confirmData();
                    }
                });
                break;
            case R.id.back_btn://返回
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        if (null != progressDialog && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        super.onDestroy();
    }
}
